package org.ank.food;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

public class EnterNameActivity extends Activity{
	
	public static final String CALL_FROM_FOLLOWING = "1";
	
	private Button nextButton,backButton, cancelButton;
	private RadioGroup genderGroup;
	private EditText nameEditText;
	private ProfileManager profileManager;
	
	@Override
	public void onCreate(Bundle b){
		super.onCreate(b);
		setContentView(R.layout.enter_name_activity);
		profileManager = ProfileManager.getInstance(getApplicationContext());
		nameEditText = (EditText)findViewById(R.id.enter_name_edittext);
		nextButton = (Button)findViewById(R.id.enter_name_next_button);
		backButton = (Button)findViewById(R.id.enter_name_back_button);
		cancelButton = (Button)findViewById(R.id.enter_name_cancel_button);
		genderGroup = (RadioGroup)findViewById(R.id.enter_name_gender_radio_group);
		String name;
		if ( ( name = profileManager.getNewProfileName() ) != null ){
			nameEditText.setText(name);
			switch (profileManager.getNewProfileGender()){
			case ProfileManager.MALE :
				genderGroup.check(R.id.enter_name_gender_radio_button_male);
				break;
			case ProfileManager.FEMALE :
				genderGroup.check(R.id.enter_name_gender_radio_button_female);
				break;
			}
		}
		else if (profileManager.getProfileMode() == ProfileManager.PROFILE_MODE_EDIT){
			nameEditText.setText(profileManager.getCurrentProfileName());
			switch (profileManager.getCurrentProfileGender()){
			case ProfileManager.MALE :
				genderGroup.check(R.id.enter_name_gender_radio_button_male);
				break;
			case ProfileManager.FEMALE :
				genderGroup.check(R.id.enter_name_gender_radio_button_female);
				break;
			}
		}
		nextButton.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				String name = nameEditText.getText().toString();
				if ( name.equals("") ){
					Toast.makeText(getApplicationContext(), R.string.enter_name_specify_name,
							Toast.LENGTH_SHORT).show();
					return;
				}
				if ( name.contains("\"") || name.contains("_") || name.contains("%") ){
					Toast.makeText(getApplicationContext(), R.string.enter_name_dont_use,
							Toast.LENGTH_LONG).show();
//					nameEditText.setText("");
					return;
				}
				if (genderGroup.getCheckedRadioButtonId() == -1){
					Toast.makeText(getApplicationContext(), R.string.enter_name_specify_gender,
							Toast.LENGTH_SHORT).show();
					return;
				}
				ProfileManager pm = ProfileManager.getInstance(getApplicationContext());
				pm.setNewProfileName(nameEditText.getText());
				switch(genderGroup.getCheckedRadioButtonId()){
					case R.id.enter_name_gender_radio_button_male :
						pm.setNewProfileGender(ProfileManager.MALE);
						break;
					case R.id.enter_name_gender_radio_button_female :
						pm.setNewProfileGender(ProfileManager.FEMALE);
						break;
				}
				Intent intent = new Intent();
        		intent.setClass(getApplicationContext(), YourAgeActivity.class);
        		intent.putExtra(CALL_FROM_FOLLOWING, false);
        		startActivity(intent);
        		finish();
			}
		});
		backButton.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				Intent intent = new Intent();
        		intent.setClass(getApplicationContext(), ProfileChooseActivity.class);
        		
        		startActivity(intent);
        		finish();
			}
		});
		cancelButton.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				if (TextUtils.isEmpty(nameEditText.getText().toString()) && 
						genderGroup.getCheckedRadioButtonId() == -1){
					Intent intent = new Intent();
					intent.setClass(getApplicationContext(), ProfileChooseActivity.class);
					startActivity(intent);
					finish();
				}else{
					AlertDialog.Builder builder = new AlertDialog.Builder(EnterNameActivity.this);
//					builder.setMessage(getString(R.string.profile_create_alert_dialog_message));
					builder.setNegativeButton(R.string.profile_create_alert_dialog_negative_button_text, 
							new DialogInterface.OnClickListener(){
						@Override
								public void onClick(DialogInterface dialog,
									int which) {
								}
							});
					builder.setPositiveButton(R.string.profile_create_alert_dialog_positive_button_text, 
							new DialogInterface.OnClickListener(){
							@Override
							public void onClick(DialogInterface dialog,	int which) {
								Intent intent = new Intent();
								intent.setClass(getApplicationContext(), ProfileChooseActivity.class);
								startActivity(intent);
								finish();
							}
						});
					builder.setTitle(R.string.profile_create_alert_dialog_title);
					builder.show();
				}
			}
			
		});
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
//			Intent intent = new Intent();
//			intent.setClass(getApplicationContext(), ProfileChooseActivity.class);
//			startActivity(intent);
//			finish();
			cancelButton.performClick();
			return true;
		}
	    return super.onKeyDown(keyCode, event);
	}
//	protected Dialog onCreateDialog(int id) {
//        AlertDialog dialog = new AlertDialog(getApplicationContext());
//    	dialog.setTitle(R.string.time_dialog_title);
//    	dialog.setMessage(getString(R.string.time_dialog_message));
//    	dialog.setOnDismissListener(dismissListener);
//        return dialog;
//    }
//    public void onPause(){
//    	super.onPause();
//    	dismissDialog(0);
//    }
//    public void onResume(){
//    	super.onResume();
//    	showDialog(0);
//    }
}
